﻿using System;
using System.Collections.Generic;
using System.Text;
using Org.Eurekaa.DNJ.Settings;
using System.ComponentModel;
using System.Web.UI;

namespace Org.Eurekaa.DNJ.UI.DNJSettings
{       
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [Serializable()]
    public class DNJAutoCompleteEvents : GenericSettingsObject
    {

        [Description("Before a request (source-option) is started, after minLength and delay are met. Can be cancelled (return false), then no request will be started and no items suggested.")]
        [DefaultValue(""), NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public UIEventFunction Search
        {
            get { return Get<UIEventFunction>("search"); }
            set { this["search"] = value; }
        }
        [Description("After a request with the data ready, before it is actually displayed; also indicates the suggestion menu will be opened.")]
        [DefaultValue(""), NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public UIEventFunction Open
        {
            get { return Get<UIEventFunction>("open"); }
            set { this["open"] = value; }
        }
        [Description("Before focus is moved to an item (not selecting), ui.item refers to the focused item. The default action of focus is to replace the text field's value with the value of the focused item. Cancelling this event prevents the value from being updated, but does not prevent the menu item from being focused.")]
        [DefaultValue(""), NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public UIEventFunction Focus
        {
            get { return Get<UIEventFunction>("focus"); }
            set { this["focus"] = value; }
        }
        [Description("Triggered when an item is selected from the menu; ui.item refers to the selected item. The default action of select is to replace the text field's value with the value of the selected item. Cancelling this event prevents the value from being updated, but does not prevent the menu from closing.")]
        [DefaultValue(""), NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public UIEventFunction Select
        {
            get { return Get<UIEventFunction>("select"); }
            set { this["select"] = value; }
        }
        [Description("When the list is hidden - doesn't have to occur together with a change.")]
        [DefaultValue(""), NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public UIEventFunction Close
        {
            get { return Get<UIEventFunction>("close"); }
            set { this["close"] = value; }
        }
        [Description("After an item was selected; ui.item refers to the selected item. Always triggered after the close event.")]
        [DefaultValue(""), NotifyParentProperty(true), RefreshProperties(RefreshProperties.Repaint)]
        [PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public UIEventFunction Change
        {
            get { return Get<UIEventFunction>("change"); }
            set { this["change"] = value; }
        }


        public DNJAutoCompleteEvents()
        {
            this["search"] = new UIEventFunction();
            this["open"] = new UIEventFunction();
            this["focus"] = new UIEventFunction();
            this["select"] = new UIEventFunction();
            this["close"] = new UIEventFunction();
            this["change"] = new UIEventFunction();
        }

        public string GetDefaultEventsScript()
        {
            String evtScript = String.Empty;
            foreach (UIEventFunction evt in Settings.Values)
            {
                if (!string.IsNullOrEmpty(evt.Name))
                {
                    evtScript += "if (!window." + evt.Name.Trim() + "){" + evt.Name.Trim() + " = new Function(\"return dnjDefaultEvent('" + evt.Name.Trim() + "')\");}";
                }
            }

            return evtScript;
        }


    }
}
